<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统配置</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(180deg, #fff8e1 0%, #e8f5e9 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }
        .navbar {
            background-color: rgba(51, 51, 51, 0.95);
            overflow: hidden;
            margin-bottom: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        .navbar a {
            float: left;
            display: block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            transition: all 0.3s;
        }
        .navbar a:hover {
            background-color: rgba(221, 221, 221, 0.9);
            color: black;
        }
        .navbar a.active {
            background-color: #4CAF50;
        }
        h1 {
            color: #333;
            margin-bottom: 30px;
            text-align: left;
        }
        h2 {
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
            margin-top: 30px;
        }
        h3 {
            margin-top: 20px;
            color: #555;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        input[type="text"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        input.changed {
            background-color: #fffacd;
            border-color: #ffd700;
        }
        button {
            padding: 8px 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 5px;
        }
        button:hover {
            background-color: #45a049;
        }
        .view-btn {
            background-color: #2196F3;
        }
        .view-btn:hover {
            background-color: #0b7dda;
        }
        .params-section {
            margin-bottom: 30px;
        }
        #message {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px;
            border-radius: 5px;
            color: white;
            display: none;
            z-index: 1000;
        }
        #message.success {
            background-color: #4CAF50;
        }
        #message.error {
            background-color: #f44336;
        }
        .header-actions {
            margin: 20px 0;
            text-align: right;
        }
        .header-actions button {
            margin-left: 10px;
        }
        .header-actions button:last-child {
            background-color: #f44336;
        }
        .header-actions button:last-child:hover {
            background-color: #d32f2f;
        }
        .last-refresh {
            margin-bottom: 20px;
            font-size: 0.8em;
            color: #555;
        }
    </style>
</head>
<body>
    <div class="navbar">
        <a href="/welcome.html">首页</a>
        <a href="/monitor.html">监控</a>
        <a href="/config.html" class="active">配置</a>
        <a href="/log.html">日志</a>
        <a href="/music.html">音乐</a>
    </div>
    
    <div class="container">
        <h1>系统配置</h1>
        
        <div class="last-refresh">
            最近刷新时间: <span id="lastRefreshTime">-</span>
        </div>
        
        <div class="header-actions">
            <button onclick="loadConfig()">刷新配置</button>
            <button onclick="logout()">退出登录</button>
        </div>
        
        <div id="message"></div>
        
        <!-- 监控配置 -->
        <div class="params-section">
            <h2>监控配置参数</h2>
            
            <h3>温度阈值参数</h3>
            <table id="tempTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>电源阈值参数</h3>
            <table id="powerThresholdTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>功率校正参数</h3>
            <table id="powerAdjustTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>其他监控参数</h3>
            <table id="otherMonitorTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
        
        <!-- MQTT配置 -->
        <div class="params-section">
            <h2>MQTT配置参数</h2>
            
            <h3>发布主题</h3>
            <table id="mqttPublishTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>订阅主题</h3>
            <table id="mqttSubscribeTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>其他MQTT配置</h3>
            <table id="mqttOtherTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
        
        <!-- 蓝牙配置 -->
        <div class="params-section">
            <h2>蓝牙配置参数</h2>
            
            <h3>扫描参数</h3>
            <table id="bluetoothScanTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>烟感设备过滤参数</h3>
            <table id="bluetoothSmokeTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>人员模拟器过滤参数</h3>
            <table id="bluetoothPersonnelTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>其他蓝牙参数</h3>
            <table id="bluetoothOtherTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
        
        <!-- Web服务器配置 -->
        <div class="params-section">
            <h2>Web服务器配置参数</h2>
            
            <h3>音频文件配置</h3>
            <table id="webserverMusicTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
            
            <h3>其他Web服务器参数</h3>
            <table id="webserverOtherTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
        
        <!-- 其他配置 -->
        <div class="params-section">
            <h2>其他配置参数</h2>
            <table id="otherTable">
                <tr>
                    <th>参数名</th>
                    <th>参数值</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 检查用户是否登录
            const username = localStorage.getItem('username');
            const userRole = localStorage.getItem('userRole');
            
            if (!username || !userRole) {
                window.location.href = '/login.html';
                return;
            }
            
            // 检查用户是否有管理员权限
            if (userRole !== 'admin') {
                window.location.href = '/welcome.html';
                return;
            }
            
            // 设置当前活动页面的样式
            const currentPath = window.location.pathname;
            document.querySelectorAll('.navbar a').forEach(link => {
                if (link.getAttribute('href') === currentPath) {
                    link.classList.add('active');
                } else {
                    link.classList.remove('active');
                }
            });
            
            // 加载配置
            loadConfig();
        });

        function showFullValue(value) {
            alert(String(value));
        }
        
        async function loadConfig() {
            try {
                console.log('开始加载配置...');
                const response = await fetch('/api/config');
                console.log('API响应状态:', response.status);
                
                if (response.status === 403) {
                    // 如果返回403，说明没有权限，跳转到监控页面
                    console.log('API返回403，没有权限');
                    alert('您没有访问配置页面的权限，将跳转到监控页面');
                    window.location.href = '/monitor.html';
                    return;
                }
                if (!response.ok) {
                    throw new Error(`服务器返回错误: ${response.status} ${response.statusText}`);
                }
                
                // 更新刷新时间
                updateRefreshTime();
                
                // 获取所有表格元素
                const tempTable = document.getElementById('tempTable');
                const powerThresholdTable = document.getElementById('powerThresholdTable');
                const powerAdjustTable = document.getElementById('powerAdjustTable');
                const otherMonitorTable = document.getElementById('otherMonitorTable');
                const mqttPublishTable = document.getElementById('mqttPublishTable');
                const mqttSubscribeTable = document.getElementById('mqttSubscribeTable');
                const mqttOtherTable = document.getElementById('mqttOtherTable');
                const bluetoothScanTable = document.getElementById('bluetoothScanTable');
                const bluetoothSmokeTable = document.getElementById('bluetoothSmokeTable');
                const bluetoothPersonnelTable = document.getElementById('bluetoothPersonnelTable');
                const bluetoothOtherTable = document.getElementById('bluetoothOtherTable');
                const webserverMusicTable = document.getElementById('webserverMusicTable');
                const webserverOtherTable = document.getElementById('webserverOtherTable');
                const otherTable = document.getElementById('otherTable');
                
                // 获取所有表格的父元素和标题
                const paramsSections = document.querySelectorAll('.params-section');
                
                // 监控配置部分
                const monitorSection = paramsSections[0];
                const monitorH3s = monitorSection ? monitorSection.querySelectorAll('h3') : [];
                const tempSection = monitorH3s[0] || null;
                const powerThresholdSection = monitorH3s[1] || null;
                const powerAdjustSection = monitorH3s[2] || null;
                const otherMonitorSection = monitorH3s[3] || null;
                
                // MQTT配置部分
                const mqttSection = paramsSections[1];
                const mqttH3s = mqttSection ? mqttSection.querySelectorAll('h3') : [];
                const mqttPublishSection = mqttH3s[0] || null;
                const mqttSubscribeSection = mqttH3s[1] || null;
                const mqttOtherSection = mqttH3s[2] || null;
                
                // 蓝牙配置部分
                const bluetoothSection = paramsSections[2];
                const bluetoothH3s = bluetoothSection ? bluetoothSection.querySelectorAll('h3') : [];
                const bluetoothScanSection = bluetoothH3s[0] || null;
                const bluetoothSmokeSection = bluetoothH3s[1] || null;
                const bluetoothPersonnelSection = bluetoothH3s[2] || null;
                const bluetoothOtherSection = bluetoothH3s[3] || null;
                
                // Web服务器配置部分
                const webserverSection = paramsSections[3];
                const webserverH3s = webserverSection ? webserverSection.querySelectorAll('h3') : [];
                const webserverMusicSection = webserverH3s[0] || null;
                const webserverOtherSection = webserverH3s[1] || null;
                
                // 其他配置部分
                const otherSection = paramsSections[4];
                
                // 初始化计数器
                let tempCount = 0;
                let powerThresholdCount = 0;
                let powerAdjustCount = 0;
                let otherMonitorCount = 0;
                let mqttPublishCount = 0;
                let mqttSubscribeCount = 0;
                let mqttOtherCount = 0;
                let bluetoothScanCount = 0;
                let bluetoothSmokeCount = 0;
                let bluetoothPersonnelCount = 0;
                let bluetoothOtherCount = 0;
                let webserverMusicCount = 0;
                let webserverOtherCount = 0;
                let otherCount = 0;
                
                // 清空所有表格
                if (tempTable) tempTable.innerHTML = '';
                if (powerThresholdTable) powerThresholdTable.innerHTML = '';
                if (powerAdjustTable) powerAdjustTable.innerHTML = '';
                if (otherMonitorTable) otherMonitorTable.innerHTML = '';
                if (mqttPublishTable) mqttPublishTable.innerHTML = '';
                if (mqttSubscribeTable) mqttSubscribeTable.innerHTML = '';
                if (mqttOtherTable) mqttOtherTable.innerHTML = '';
                if (bluetoothScanTable) bluetoothScanTable.innerHTML = '';
                if (bluetoothSmokeTable) bluetoothSmokeTable.innerHTML = '';
                if (bluetoothPersonnelTable) bluetoothPersonnelTable.innerHTML = '';
                if (bluetoothOtherTable) bluetoothOtherTable.innerHTML = '';
                if (webserverMusicTable) webserverMusicTable.innerHTML = '';
                if (webserverOtherTable) webserverOtherTable.innerHTML = '';
                if (otherTable) otherTable.innerHTML = '';

                // 获取所有配置
                const data = await response.json();
                if (!data) {
                    throw new Error('获取到的配置数据为空');
                }
                
                // 遍历配置项并分类
                for (const key in data) {
                    const value = data[key];
                    const row = document.createElement('tr');
                    
                    const keyCell = document.createElement('td');
                    keyCell.textContent = key;
                    row.appendChild(keyCell);
                    
                    const valueCell = document.createElement('td');
                    const input = document.createElement('input');
                    input.type = 'text';
                    input.value = value;
                    input.dataset.key = key;
                    input.addEventListener('change', handleInputChange);
                    valueCell.appendChild(input);
                    row.appendChild(valueCell);
                    
                    // 添加查看按钮和保存按钮
                    const actionCell = document.createElement('td');
                    const viewBtn = document.createElement('button');
                    viewBtn.className = 'view-btn';
                    viewBtn.textContent = '查看';
                    viewBtn.addEventListener('click', () => showFullValue(value));
                    
                    const saveBtn = document.createElement('button');
                    saveBtn.textContent = '保存';
                    saveBtn.addEventListener('click', () => updateConfig(key, input));
                    
                    actionCell.appendChild(viewBtn);
                    actionCell.appendChild(saveBtn);
                    row.appendChild(actionCell);
                    
                    // 根据参数名称分类
                    if (key.startsWith('monitor.temp_threshold.')) {
                        if (tempTable) tempTable.appendChild(row);
                        tempCount++;
                    } else if (key.startsWith('monitor.power_adjust.')) {
                        if (powerAdjustTable) powerAdjustTable.appendChild(row);
                        powerAdjustCount++;
                    } else if (key.startsWith('monitor.power.') || key.startsWith('monitor.threshold.')) {
                        if (powerThresholdTable) powerThresholdTable.appendChild(row);
                        powerThresholdCount++;
                    } else if (key.startsWith('monitor.')) {
                        if (otherMonitorTable) otherMonitorTable.appendChild(row);
                        otherMonitorCount++;
                    } else if (key.startsWith('mqtt.publish.')) {
                        if (mqttPublishTable) mqttPublishTable.appendChild(row);
                        mqttPublishCount++;
                    } else if (key.startsWith('mqtt.subscribe.')) {
                        if (mqttSubscribeTable) mqttSubscribeTable.appendChild(row);
                        mqttSubscribeCount++;
                    } else if (key.startsWith('mqtt.')) {
                        if (mqttOtherTable) mqttOtherTable.appendChild(row);
                        mqttOtherCount++;
                    } else if (key.startsWith('bluetooth.scan.') || key.startsWith('bluetooth.expire.')) {
                        if (bluetoothScanTable) bluetoothScanTable.appendChild(row);
                        bluetoothScanCount++;
                    } else if (key.startsWith('bluetooth.smoke_filter.')) {
                        if (bluetoothSmokeTable) bluetoothSmokeTable.appendChild(row);
                        bluetoothSmokeCount++;
                    } else if (key.startsWith('bluetooth.personnel_filter.')) {
                        if (bluetoothPersonnelTable) bluetoothPersonnelTable.appendChild(row);
                        bluetoothPersonnelCount++;
                    } else if (key.startsWith('bluetooth.')) {
                        if (bluetoothOtherTable) bluetoothOtherTable.appendChild(row);
                        bluetoothOtherCount++;
                    } else if (key.startsWith('webserver.music.')) {
                        if (webserverMusicTable) webserverMusicTable.appendChild(row);
                        webserverMusicCount++;
                    } else if (key.startsWith('webserver.')) {
                        if (webserverOtherTable) webserverOtherTable.appendChild(row);
                        webserverOtherCount++;
                    } else {
                        if (otherTable) otherTable.appendChild(row);
                        otherCount++;
                    }
                }

                // 根据计数器决定是否显示各个部分
                if (tempCount === 0) {
                    if (tempSection) tempSection.style.display = 'none';
                    if (tempTable) tempTable.style.display = 'none';
                } else {
                    if (tempSection) tempSection.style.display = 'block';
                    if (tempTable) tempTable.style.display = 'table';
                }
                
                if (powerThresholdCount === 0) {
                    if (powerThresholdSection) powerThresholdSection.style.display = 'none';
                    if (powerThresholdTable) powerThresholdTable.style.display = 'none';
                } else {
                    if (powerThresholdSection) powerThresholdSection.style.display = 'block';
                    if (powerThresholdTable) powerThresholdTable.style.display = 'table';
                }
                
                if (powerAdjustCount === 0) {
                    if (powerAdjustSection) powerAdjustSection.style.display = 'none';
                    if (powerAdjustTable) powerAdjustTable.style.display = 'none';
                } else {
                    if (powerAdjustSection) powerAdjustSection.style.display = 'block';
                    if (powerAdjustTable) powerAdjustTable.style.display = 'table';
                }
                
                if (otherMonitorCount === 0) {
                    if (otherMonitorSection) otherMonitorSection.style.display = 'none';
                    if (otherMonitorTable) otherMonitorTable.style.display = 'none';
                } else {
                    if (otherMonitorSection) otherMonitorSection.style.display = 'block';
                    if (otherMonitorTable) otherMonitorTable.style.display = 'table';
                }
                
                if (mqttPublishCount === 0) {
                    if (mqttPublishSection) mqttPublishSection.style.display = 'none';
                    if (mqttPublishTable) mqttPublishTable.style.display = 'none';
                } else {
                    if (mqttPublishSection) mqttPublishSection.style.display = 'block';
                    if (mqttPublishTable) mqttPublishTable.style.display = 'table';
                }
                
                if (mqttSubscribeCount === 0) {
                    if (mqttSubscribeSection) mqttSubscribeSection.style.display = 'none';
                    if (mqttSubscribeTable) mqttSubscribeTable.style.display = 'none';
                } else {
                    if (mqttSubscribeSection) mqttSubscribeSection.style.display = 'block';
                    if (mqttSubscribeTable) mqttSubscribeTable.style.display = 'table';
                }
                
                if (mqttOtherCount === 0) {
                    if (mqttOtherSection) mqttOtherSection.style.display = 'none';
                    if (mqttOtherTable) mqttOtherTable.style.display = 'none';
                } else {
                    if (mqttOtherSection) mqttOtherSection.style.display = 'block';
                    if (mqttOtherTable) mqttOtherTable.style.display = 'table';
                }
                
                if (bluetoothScanCount === 0) {
                    if (bluetoothScanSection) bluetoothScanSection.style.display = 'none';
                    if (bluetoothScanTable) bluetoothScanTable.style.display = 'none';
                } else {
                    if (bluetoothScanSection) bluetoothScanSection.style.display = 'block';
                    if (bluetoothScanTable) bluetoothScanTable.style.display = 'table';
                }
                
                if (bluetoothSmokeCount === 0) {
                    if (bluetoothSmokeSection) bluetoothSmokeSection.style.display = 'none';
                    if (bluetoothSmokeTable) bluetoothSmokeTable.style.display = 'none';
                } else {
                    if (bluetoothSmokeSection) bluetoothSmokeSection.style.display = 'block';
                    if (bluetoothSmokeTable) bluetoothSmokeTable.style.display = 'table';
                }
                
                if (bluetoothPersonnelCount === 0) {
                    if (bluetoothPersonnelSection) bluetoothPersonnelSection.style.display = 'none';
                    if (bluetoothPersonnelTable) bluetoothPersonnelTable.style.display = 'none';
                } else {
                    if (bluetoothPersonnelSection) bluetoothPersonnelSection.style.display = 'block';
                    if (bluetoothPersonnelTable) bluetoothPersonnelTable.style.display = 'table';
                }
                
                if (bluetoothOtherCount === 0) {
                    if (bluetoothOtherSection) bluetoothOtherSection.style.display = 'none';
                    if (bluetoothOtherTable) bluetoothOtherTable.style.display = 'none';
                } else {
                    if (bluetoothOtherSection) bluetoothOtherSection.style.display = 'block';
                    if (bluetoothOtherTable) bluetoothOtherTable.style.display = 'table';
                }
                
                if (webserverMusicCount === 0) {
                    if (webserverMusicSection) webserverMusicSection.style.display = 'none';
                    if (webserverMusicTable) webserverMusicTable.style.display = 'none';
                } else {
                    if (webserverMusicSection) webserverMusicSection.style.display = 'block';
                    if (webserverMusicTable) webserverMusicTable.style.display = 'table';
                }
                
                if (webserverOtherCount === 0) {
                    if (webserverOtherSection) webserverOtherSection.style.display = 'none';
                    if (webserverOtherTable) webserverOtherTable.style.display = 'none';
                } else {
                    if (webserverOtherSection) webserverOtherSection.style.display = 'block';
                    if (webserverOtherTable) webserverOtherTable.style.display = 'table';
                }
                
                // 检查整个分类是否为空
                if (tempCount === 0 && powerThresholdCount === 0 && powerAdjustCount === 0 && otherMonitorCount === 0) {
                    if (monitorSection) monitorSection.style.display = 'none';
                } else {
                    if (monitorSection) monitorSection.style.display = 'block';
                }
                
                if (mqttPublishCount === 0 && mqttSubscribeCount === 0 && mqttOtherCount === 0) {
                    if (mqttSection) mqttSection.style.display = 'none';
                } else {
                    if (mqttSection) mqttSection.style.display = 'block';
                }
                
                if (bluetoothScanCount === 0 && bluetoothSmokeCount === 0 && bluetoothPersonnelCount === 0 && bluetoothOtherCount === 0) {
                    if (bluetoothSection) bluetoothSection.style.display = 'none';
                } else {
                    if (bluetoothSection) bluetoothSection.style.display = 'block';
                }
                
                if (webserverMusicCount === 0 && webserverOtherCount === 0) {
                    if (webserverSection) webserverSection.style.display = 'none';
                } else {
                    if (webserverSection) webserverSection.style.display = 'block';
                }
                
                if (otherCount === 0) {
                    if (otherSection) otherSection.style.display = 'none';
                } else {
                    if (otherSection) otherSection.style.display = 'block';
                }
                
                showMessage('配置加载成功', 'success');
            } catch (error) {
                console.error('加载配置失败:', error);
                showMessage('加载配置失败: ' + error.message, 'error');
            }
        }

        function handleInputChange(event) {
            const input = event.target;
            input.classList.add('changed');
        }

        async function updateConfig(key, inputElement) {
            const value = inputElement.value;
            console.log(`正在更新配置: ${key} = ${value}`);
            
            try {
                console.log('发送更新请求...');
                const response = await fetch('/api/config', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        key,
                        value: value
                    })
                });
                
                console.log(`收到响应: 状态=${response.status}, 状态文本=${response.statusText}`);
                
                if (!response.ok) {
                    throw new Error(`服务器返回错误: ${response.status} ${response.statusText}`);
                }
                
                // 尝试解析JSON响应
                let responseText = '';
                try {
                    responseText = await response.text();
                    if (responseText.trim()) {
                        const data = JSON.parse(responseText);
                        inputElement.classList.remove('changed');
                        showMessage(data?.message || '配置更新成功', 'success');
                    } else {
                        inputElement.classList.remove('changed');
                        showMessage('配置更新成功', 'success');
                    }
                } catch (jsonError) {
                    console.error('解析响应JSON失败:', jsonError, '原始响应:', responseText);
                    inputElement.classList.remove('changed');
                    showMessage('配置已更新，但响应格式异常', 'success');
                }
            } catch (error) {
                console.error('更新配置失败:', error);
                showMessage('更新失败: ' + error.message, 'error');
            }
        }

        // 显示消息
        function showMessage(message, type) {
            const messageElement = document.getElementById('message');
            messageElement.textContent = message;
            messageElement.className = type;
            messageElement.style.display = 'block';
            
            setTimeout(() => {
                messageElement.style.display = 'none';
            }, 3000);
        }
        
        // 更新刷新时间
        function updateRefreshTime() {
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const day = String(now.getDate()).padStart(2, '0');
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            const seconds = String(now.getSeconds()).padStart(2, '0');
            
            const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            
            const lastRefreshTimeElement = document.getElementById('lastRefreshTime');
            if (lastRefreshTimeElement) {
                lastRefreshTimeElement.textContent = formattedTime;
            }
        }
        
        // 退出登录
        async function logout() {
            try {
                const response = await fetch('/api/logout', {
                    method: 'POST'
                });
                
                if (response.ok) {
                    window.location.href = '/login.html';
                } else {
                    showMessage('退出失败: ' + (await response.text()), 'error');
                }
            } catch (error) {
                showMessage('退出失败: ' + error.message, 'error');
            }
        }
    </script>
</body>
</html>